package com.qf.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;


/**
 * @Description: 公用常量
 * @Data: 2022/9/14 10:25
 * @Pacakge: com.qf.aop
 * @ClassName: MyAspect
 * @Author: fan
 */
@Aspect
public class MyAspect {
    //声明日志对象
    private static final Logger logger = LoggerFactory.getLogger(MyAspect.class);

    //定义切点
    @Pointcut(value = "execution(* *.*.*.*.*(..))")
    public void myPointcut(){

    }
    //环绕通知
    @Around("myPointcut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint){
        logger.debug("开始记录日志:"+new Date());
        //获取到执行方法
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        logger.debug("执行方法是:"+methodSignature.getMethod());
        //执行执行方法
        Object proceed = null;
        try {
            proceed = proceedingJoinPoint.proceed();
            logger.debug("执行方法是:"+methodSignature.getMethod()+"执行完成");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            logger.error("执行方法是"+methodSignature.getMethod()+"执行异常");
        }finally{
            logger.debug("执行时间是:"+new Date());
        }
        return proceed;
    }
}
